const axios = require('axios');

const BASE_URL = 'http://localhost:3000/api';

async function testUserRegistration() {
  console.log('=== 测试用户注册 ===');
  try {
    const response = await axios.post(`${BASE_URL}/auth/register`, {
      username: 'debuguser',
      email: 'debug@example.com',
      password: '123456'
    });
    console.log('注册成功:', response.data);
    return response.data.token;
  } catch (error) {
    console.error('注册失败:');
    console.error('状态码:', error.response?.status);
    console.error('错误信息:', error.response?.data);
    console.error('详细错误:', error.message);
    return null;
  }
}

async function testUserLogin() {
  console.log('\n=== 测试用户登录 ===');
  try {
    const response = await axios.post(`${BASE_URL}/auth/login`, {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    console.log('登录成功:', response.data);
    return response.data.token;
  } catch (error) {
    console.error('登录失败:');
    console.error('状态码:', error.response?.status);
    console.error('错误信息:', error.response?.data);
    return null;
  }
}

async function testGetCurrentUser(token) {
  console.log('\n=== 测试获取当前用户信息 ===');
  try {
    const response = await axios.get(`${BASE_URL}/auth/me`, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });
    console.log('获取用户信息成功:', response.data);
  } catch (error) {
    console.error('获取用户信息失败:');
    console.error('状态码:', error.response?.status);
    console.error('错误信息:', error.response?.data);
  }
}

async function testGetUserProfile(token) {
  console.log('\n=== 测试获取用户资料 ===');
  try {
    const response = await axios.get(`${BASE_URL}/users/profile`, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });
    console.log('获取用户资料成功:', response.data);
  } catch (error) {
    console.error('获取用户资料失败:');
    console.error('状态码:', error.response?.status);
    console.error('错误信息:', error.response?.data);
  }
}

async function testJoinActivity(token, activityId) {
  console.log(`\n=== 测试参与活动 ${activityId} ===`);
  try {
    const response = await axios.post(`${BASE_URL}/activities/${activityId}/join`, {}, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });
    console.log('参与活动成功:', response.data);
  } catch (error) {
    console.error('参与活动失败:');
    console.error('状态码:', error.response?.status);
    console.error('错误信息:', error.response?.data);
    if (error.response?.data?.details) {
      console.error('详细错误:', error.response.data.details);
    }
  }
}

async function testGetActivities() {
  console.log('\n=== 测试获取活动列表 ===');
  try {
    const response = await axios.get(`${BASE_URL}/activities`);
    console.log('获取活动列表成功，活动数量:', response.data.activities.length);
    if (response.data.activities.length > 0) {
      console.log('第一个活动:', {
        id: response.data.activities[0].id,
        title: response.data.activities[0].title,
        current_participants: response.data.activities[0].current_participants,
        max_participants: response.data.activities[0].max_participants
      });
      return response.data.activities[0].id;
    }
    return null;
  } catch (error) {
    console.error('获取活动列表失败:');
    console.error('状态码:', error.response?.status);
    console.error('错误信息:', error.response?.data);
    return null;
  }
}

async function main() {
  console.log('开始API调试...\n');
  
  // 测试注册
  const registerToken = await testUserRegistration();
  
  // 测试登录
  const loginToken = await testUserLogin();
  
  if (loginToken) {
    // 测试获取当前用户信息
    await testGetCurrentUser(loginToken);
    
    // 测试获取用户资料
    await testGetUserProfile(loginToken);
    
    // 测试获取活动列表
    const firstActivityId = await testGetActivities();
    
    // 测试参与活动
    if (firstActivityId) {
      await testJoinActivity(loginToken, firstActivityId);
    }
  }
  
  console.log('\n调试完成');
}

main().catch(console.error); 